﻿using ELTE.SEM.Data.ViewModels;
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using WinSCP;

namespace ELTE.SEM.Data.Connectors
{
    public class SambaConnector
    {
        private FolderDataConnector folderData;
        private SftpClient client;
        private SemFileServerLoginViewModel loginInfoModel;

        public SambaConnector(SemFileServerLoginViewModel model)
        {
            folderData = new FolderDataConnector();
            client = new SftpClient(new PasswordConnectionInfo(model.Server, model.UserName, model.Password));
            loginInfoModel = model;
        }

        public List<SemDataViewModel> GetAllFolders()
        {
            List<SemDataViewModel> folders = new List<SemDataViewModel>();
            try
            {

                client.Connect();

                folders = folderData.GetDirectoriesSSH(client, loginInfoModel.HomeFolder);

                client.Disconnect();
            }
            catch (Exception ex)
            {
                //TODO: log error
            }
            return folders;
        }

        public List<string> GetFilePathsInFolder(SemFolderViewModel folderModel, string rootPath, string extension)
        {
            return folderData.GetFilesInFolder(client, folderModel, rootPath, extension);
        }

        public Stream GetFileStream(string filePath)
        {
            Stream result = new MemoryStream();
            try
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
                string fPath = filePath.Replace("/repdata/Q3D", "/home/mszolnoki");
                client.DownloadFile(fPath, result);
                return result;
            }
            catch (Exception ex)
            {
                //TODO: log error
            }
            return result;

        }


    }
}
